package com.hunan.api.catv.mq.consumer;

import com.google.gson.JsonObject;
import com.hunan.api.catv.entity.enums.LikeType;
import com.hunan.api.catv.entity.vo.MemberLikeVo;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.like.MemberLikeBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class LikeConsumer {

    public static Logger log = LoggerFactory.getLogger(LikeConsumer.class);

    @Autowired
    MemberLikeBiz memberLikeBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.like_queue, durable = "true"), key = MqConstants.like_routingkey, exchange = @Exchange(value = MqConstants.like_exchange, type = ExchangeTypes.TOPIC)))
    public void like(String like) {
        JsonObject likeData = GsonUtils.fromJson(like, JsonObject.class);
        Integer collectType = likeData.get("collectType").getAsInt();
        Integer itemId = likeData.get("itemId").getAsInt();
        Integer memberId = likeData.get("memberId").getAsInt();
        Integer type = likeData.get("type").getAsInt();
        LikeType likeType = LikeType.getResult(collectType);
        switch (likeType) {
            case like:
                MemberLikeVo memberLike = memberLikeBiz.info(memberId, itemId, type);
                if (ObjectUtils.isEmpty(memberLike)) {
                    memberLikeBiz.add(type, itemId, memberId);
                }
                break;
            case cancleLike:
                memberLikeBiz.cancel(type, itemId, memberId);
                break;
            default:
                break;
        }
    }

}
